const express = require('express');
const dayjs = require('dayjs')
const router = express.Router();
const {dictModel} = require('../../db/dictMod');
const { localDate} = require('../../serverOpt');

// 分页查询
router.get('/pageList', async (req, res) => {
    try {
        const { page = 1, limit = 1000, name } = req.query;
        
        // 构建查询条件
        const conditions = {};
        if (name) conditions.name = new RegExp(name, 'i'); // 模糊查询

        // 计算总数
        const total = await dictModel.countDocuments(conditions);
        
        // 查询数据
        const list = await dictModel.find(conditions)
            .lean()
            .skip((page - 1) * limit)
            .limit(Number(limit))
            .sort({ createTime: -1 });

        res.json({
            code: 0,
            data: {
                list,
                total,
                page: Number(page),
                limit: Number(limit)
            },
            msg: '查询成功'
        });
    } catch (error) {
        res.json({
            code: -1,
            msg: error.message
        });
    }
});

// 添加
router.post('/add', async (req, res) => {
    try {
        const { name, value } = req.body;

        // 验证必填字段
        if (!name) {
            return res.json({
                code: -1,
                msg: '名称不能为空'
            });
        }

        if (!value) {
            return res.json({
                code: -1,
                msg: '值不能为空'
            });
        }

        const data = {
            name,
            value,
            createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        };
        
        const result = await dictModel.create(data);
        res.json({
            code: 0,
            data: result,
            msg: '添加成功'
        });
    } catch (error) {
        res.json({
            code: -1,
            msg: error.message
        });
    }
});

// 更新
router.post('/update', async (req, res) => {
    try {
        const { _id, name, value } = req.body;
        
        if (!_id) {
            return res.json({
                code: -1,
                msg: '缺少_id参数'
            });
        }

        if (!name) {
            return res.json({
                code: -1,
                msg: '名称不能为空'
            });
        }

        if (!value) {
            return res.json({
                code: -1,
                msg: '值不能为空'
            });
        }

        const updateData = {
            name,
            value
        };

        const result = await dictModel.findByIdAndUpdate(
            _id,
            updateData,
            { new: true }
        );

        if (!result) {
            return res.json({
                code: -1,
                msg: '未找到要更新的数据'
            });
        }

        res.json({
            code: 0,
            data: result,
            msg: '更新成功'
        });
    } catch (error) {
        res.json({
            code: -1,
            msg: error.message
        });
    }
});

// 删除
router.post('/delete', async (req, res) => {
    try {
        const { _id } = req.body;
        
        if (!_id) {
            return res.json({
                code: -1,
                msg: '缺少_id参数'
            });
        }

        const result = await dictModel.findByIdAndDelete(_id);

        if (!result) {
            return res.json({
                code: -1,
                msg: '未找到要删除的数据'
            });
        }

        res.json({
            code: 0,
            msg: '删除成功'
        });
    } catch (error) {
        res.json({
            code: -1,
            msg: error.message
        });
    }
});

module.exports = router;